<?php
/**
 * 用户绑定卡片
 * User: Acer
 * Date: 2022/7/9
 * Time: 23:27
 */

namespace app\move\controller;

use app\core\MyController as MyController;
use app\move\service\CardUserService;
use think\Log;
class CardUser extends MyController
{
    protected $noNeedAction = []; // 不需要校验控制器，通道
    // 操作前置
    protected $beforeActionList = ['my_initialize'];

    // 操作前置方法
    public function my_initialize()
    {
    }

    // 发送验证码
    public function send_vcode(){
        $data = input();
        if(empty($data['phone'])) json_err(-201);
        // 手机号绑定到指定用户
        $installData = [
            'uid'   =>  $this->userId,
            'phone' =>  $data['phone']
        ];
        $res = (new CardUserService())->s_send_vcode($installData);
        if($res) _action_result(200,[],'操作成功！');
        json_err( -205);
    }

    // 手机验证码认证
    public function bind_phone(){
        $data = input();
        if(empty($data['phone']) || empty($data['vcode'])) json_err(-201);
        // 手机号绑定到指定用户
        $installData = [
            'uid'   =>  $this->userId,
            'vcode' =>  $data['vcode'],
            'phone' =>  $data['phone']
        ];
        $res = (new CardUserService())->user_bind_phone($installData);
        if($res) _action_result(200,[],'操作成功！');
        json_err( -205);
    }

    // 修改绑定手机号
    public function modify_phone(){
        $data = input();
        if(empty($data['phone']) || empty($data['vcode'])) json_err(-201);
        // 手机号绑定到指定用户
        $installData = [
            'uid'   =>  $this->userId,
            'vcode' =>  $data['vcode'],
            'phone' =>  $data['phone']
        ];
        $res = (new CardUserService())->modify_bind_phone($installData);
        if($res) _action_result(200,[],'操作成功！');
        json_err( -205);
    }

  // 激活卡片
    public function bind_active(){
        $data = input();
        if(empty($data['card_code'])) json_err(-201);

        if(isset($data['type']) && $data['type']==1){
            //為本人激活

            $installData = [
                'uid'   =>$this->userId, // 获取当前uid
                'card_code' =>  $data['card_code'],
                'is_other'=>null,
                'policy_no'=>'',// 保单号
                'insurance_carriers'=>''// 承保公司
            ];
        }else if(isset($data['type']) && $data['type']==2){
            //為他人激活
	    if(!isset($data['equity_mobile'])){
                $data['equity_mobile']='';
            }
            $installData = [
                'uid'   =>$this->userId, // 获取当前uid
                'card_code' =>  $data['card_code'],
                'equity_username' => $data['equity_username'],//權益人姓名
                // 'equity_idtype' => $data['equity_idtype'],//權益人證件類型
                'equity_idcard' => $data['equity_idcard'],//證件號
                'equity_mobile' => $data['equity_mobile'],//權益人手機號
                 'is_other'=>1,
                'policy_no'=>'',// 保单号
                'insurance_carriers'=>''// 承保公司
            ];
        }else{
            json_err(-201);
        }
        if(isset($data['manager_phone']) && !empty($data['manager_phone'])){
            $installData['manager_phone'] = $data['manager_phone'];
        }
        if(isset($data['policy_no']) && !empty($data['policy_no'])){
            $installData['policy_no'] = $data['policy_no'];
        }
        if(isset($data['insurance_carriers']) && !empty($data['insurance_carriers'])){
            $installData['insurance_carriers'] = $data['insurance_carriers'];
        }
        $res = (new CardUserService())->s_bind_active($installData);
        if($res) _action_result(200,[],'操作成功！');
        json_err( -205);
    }

    // 安护无忧卡叠加
    public function bind_active_append(){
        $data = input();
        if(empty($data['cu_id']) or empty($data['card_code'])) json_err(-201);
        $data['uid'] = $this->userId;
        $res = (new CardUserService())->bind_active_append($data);
        if($res) _action_result(200,[],'操作成功！');
        json_err( -205);
    }

    // 获取卡片绑定的成员
    public function get_card_user_list(){
        $data = input();
        if(empty($data['pid'])) json_err(-201);
        // 获取成员类型
        $type = null;
        if(isset($data['type']))$type = $data['type'];
        $uid = $this->userId;
        $res = (new CardUserService())->s_get_card_user_list($data['pid'],$uid,$type);
         _action_result(200,$res,'操作成功！');
    }

    // 卡片设置保存
    public function card_set(){
        $data = input();
        if(empty($data['fid'])) json_err(-201);
        $res = (new CardUserService())->s_card_set($data['fid']);
        if($res) _action_result(200,[],'操作成功！');
        json_err( -205);
    }

//
public function huiyi(){
        $res = (new CardUserService())->huiyi_active([]);
        if($res) _action_result(200,[],'操作成功！');
        json_err( -205);
    }

    // 绑定保单号
    public function bind_policy_no(){
        $data = input();
        if(empty($data['cu_id'])) json_err(-201);
        $data['uid'] = $this->userId;
        $res = (new CardUserService())->bind_policy_no($data);
        if($res) _action_result(200,[],'操作成功！');
        json_err( -205);
    }

}
